package com.ltmonitor.service.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ltmonitor.dao.IDepartmentDao;
import com.ltmonitor.entity.Department;
import com.ltmonitor.service.IDepartmentService;

@Service("departmentService")
public class DepartmentService extends BaseService<Long, Department> implements IDepartmentService {

	@Autowired
	private IDepartmentDao departmentDao;

	/**
	 * 部门基本信息缓存
	 */
	public static HashMap<Long, Department> depMap = new HashMap<Long, Department>();

	protected Logger logger = Logger.getLogger(DepartmentService.class);

	public void init() {
		String hql = "from Department where deleted = ?";
		List result = query(hql, false);
		for (Object obj : result) {
			Department bd = (Department) obj;
			depMap.put(bd.getEntityId(), bd);
		}
	}
	
	@Override
	public Collection<Department> loadAll()
	{
		return depMap.values();
	}
	
	public ArrayList<Department> fetchDepartmentList(List<Long> depIdList)
	{
		ArrayList<Department> depList = new ArrayList<Department>();
		for (long depId : depIdList) {
			Department dep = this.fetchDepartment(depId);
			if (dep == null || dep.getDeleted())
				continue;
			depList.add(dep);
		}
		return depList;
	}

	public Department fetchDepartment(long depId) {
		if (depId <= 0)
			return null;
		if (depMap.containsKey(depId)) {
			return depMap.get(depId);
		}
		try {
			Department d = (Department) this.load(depId);
			if (d!=null && d.getDeleted() == false) {
				depMap.put(depId, d);
				return d;
			}
		} catch (Exception ex) {
			logger.error(ex.getMessage(), ex);
		}
		return null;
	}
	
	public List<Department> getDepartmentListByUserId(long userId)
	{
		String hql = "select c.departments from UserInfo c where c.entityId = ?";
		List<Department> depList = this.query(hql, userId);
		return depList;
	}

	public void saveDepartment(Department dep) throws Exception {
		String hsql = "from Department where (name = ?) and entityId <> ? and deleted = ? ";
		Department otherVd = (Department) baseDao.find(hsql,
				new Object[] { dep.getName(), dep.getEntityId(), false });
		if (otherVd != null) {
			String msg = "部门名称重复，无法保存!";
			throw new Exception(msg);
		}

		baseDao.saveOrUpdate(dep);
		depMap.put(dep.getEntityId(), dep);
	}	
	

	/**
	 * 查找车组下面所有的级联的子车组
	 */
	public List<Long> getDepIdList(long depId) {
		List<Long> depIdList = new ArrayList<Long>();
		List<Long> allList = new ArrayList<Long>();
		depIdList.add(depId);
		allList.add(depId);
		while (depIdList.size() > 0) {
			depIdList = getChildDepIdList(depIdList, allList);
		}

		return allList;
	}

	public List<Long> getParentDepIdList(long depId) {
		List<Long> allList = new ArrayList<Long>();
		allList.add(depId);
		if (depId > 0) {
			Department d = this.fetchDepartment(depId);
			if (d != null) {
				long parentId = d.getParentId();
				while (parentId > 0) {
					allList.add(parentId);
					Department p = this.fetchDepartment(parentId);
					parentId = p != null ? p.getParentId() : 0;
				}
			}
		}

		return allList;
	}

	private List<Long> getChildDepIdList(List<Long> parentIdList,
			List<Long> allList) {
		String hql = "from Department where parentId in (:depIdList) and deleted = 0";

		List result = baseDao.queryByNamedParam(hql, "depIdList",
				parentIdList.toArray());
		List<Long> depIdList = new ArrayList<Long>();
		for (Object obj : result) {
			Department dep = (Department) obj;
			allList.add(dep.getEntityId());
			depIdList.add(dep.getEntityId());
		}
		return depIdList;
	}


}
